#include "Friend.h"

PlayerFriend::PlayerFriend()
{
    mIsLoadOfflineRequest = false;
}

void PlayerFriend::unpackFromJson(const rapidjson::Value& json)
{
    rapidjson::Value::ConstMemberIterator friendsIt = json.FindMember("friends");
    if (friendsIt != json.MemberEnd())
    {
        const rapidjson::Value& friendsInfo = friendsIt->value;
        if (friendsInfo.IsArray())
        {
            for (int i = 0; i < friendsInfo.Size(); i++)
            {
                int64_t uid = friendsInfo[i].FindMember("uid")->value.GetInt64();
                mFriendUIDS.insert(uid);
            }
        }
    }

    rapidjson::Value::ConstMemberIterator addRequestIt = json.FindMember("addRequest");
    if (addRequestIt != json.MemberEnd())
    {
        const rapidjson::Value& addRequestInfo = addRequestIt->value;
        for (int i = 0; i < addRequestInfo.Size(); i++)
        {
            int64_t uid = addRequestInfo[i].GetInt64();
            mRequestAddUIDS.insert(uid);
        }
    }
}

rapidjson::Value PlayerFriend::packToJson(rapidjson::Document& doc) const
{
    rapidjson::Value value(rapidjson::kObjectType);
    rapidjson::Value friendsInfo(rapidjson::kArrayType);
    rapidjson::Value requestAddUID(rapidjson::kArrayType);

    for (auto& v : mFriendUIDS)
    {
        rapidjson::Value oneFriend(rapidjson::kObjectType);
        oneFriend.AddMember("uid", v, doc.GetAllocator());
        friendsInfo.PushBack(oneFriend, doc.GetAllocator());
    }

    for (auto& v : mRequestAddUIDS)
    {
        requestAddUID.PushBack(v, doc.GetAllocator());
    }

    value.AddMember("friends", friendsInfo, doc.GetAllocator());
    value.AddMember("addRequest", requestAddUID, doc.GetAllocator());
    return value;
}

void PlayerFriend::setLoadOfflineRequestStatus(bool value)
{
    mIsLoadOfflineRequest = value;
}

bool PlayerFriend::isLoadOfflineRequest() const
{
    return mIsLoadOfflineRequest;
}

set<int64_t>& PlayerFriend::getFriendUIDS()
{
    return mFriendUIDS;
}

set<int64_t>& PlayerFriend::getOnlineFriendUIDS()
{
    return mOnlineFriendUIDS;
}

set<int64_t>& PlayerFriend::getRequestUIDS()
{
    return mRequestAddUIDS;
}